<?php
session_start();
include_once("model/user.model.php");
include_once("model/vacancy.model.php");
include_once("model/transaction.model.php");

//-->functions for users
function register_form() 
{
	if(!isset($_SESSION['username']))
	{
		$name = $_GET['name'];
		$form .= "<img src='image/banners/register.png'><br>";
		$form .= "<form name='myform' method = 'post' dojoType='dijit.form.Form' action = 'do_register.php' execute='submitIfValid(this.isValid())'>";
		$form .= "<table>";
		$form .= "<tr><td>Name:</td><td><input type='text' disabled='true' name='name' value='" .$name. "' dojotype='dijit.form.ValidationTextBox' required='true' promptmessage= 'Please click Check Name to fill in user name' />
			<input type='hidden' name='name' value='" .$name. "' dojotype='dijit.form.ValidationTextBox' required='true' promptmessage= 'Please click Check Name to fill in user name' /></td>";
		$form .= "<td><a href=javascript:createWindow('check_valid_user.php',";
		$form .= "'window2',";
		$form .= "'width=400,height=200,status,location')>Check Name</a></td></tr>";
		$form .= "<tr><td>Password:</td><td><input type = 'password' name = 'password' id='password'  dojotype='dijit.form.ValidationTextBox' minlength='6' required='true' promptmessage= 'Enter Your Password (Min : 6 words)'></td></tr>";
		$form .= "<tr><td>Confirm Password:</td><td><input type = 'password' name = 'password2' id='password2' dojotype='dijit.form.ValidationTextBox' minlength='6' required='true' promptmessage= 'Enter Your Password (Min : 6 words)'></td></tr>";
        $form .= "<tr><td>Gender:</td><td><select name='gender' id='gender' dojoType='dijit.form.FilteringSelect'
                  autocomplete='false'>
                  <option value='M'>Male</option>
                  <option value='F'>Female</option>
                  </select></td>";
		$form .= "<tr><td>Email:</td><td><input type = 'text' name = 'email' dojoType='dijit.form.ValidationTextBox'
				regExp='[\w]+@[\w]+.[\w]+'
				required='true'
				invalidMessage='Wrong Email format'></td></tr>";
		$form .= "<tr><td>Handphone No:</td><td><input type = 'text' name = 'hp' dojoType='dijit.form.ValidationTextBox'
				regExp='\d{8}'
				required='true'
				invalidMessage='Invalid Phone number.'></td></tr>";
		$form .= "<tr><td>IC:</td><td><input type = 'text' name = 'ic' dojoType='dijit.form.ValidationTextBox'
				maxlength='9' minlength='9'
				required='true'
				invalidMessage='Invalid IC.'></td></tr>";
		$form .= "<tr><td>Member Type:</td><td><input type = 'radio' name = 'type' value=2 dojoType='dijit.form.RadioButton'>Landlord</input><br>
		<input type = 'radio' name = 'type' value=3 dojoType='dijit.form.RadioButton'>Tenant</input></td></tr>";
		$form .= "<tr><td></td><td><input type = 'submit' onclick='return checkAllPassword(myform); return false;' value = 'Submit'></td></tr></table></form>";
		return $form;
    } else {
        return "You are not allow to register again!<a href='index.php'>Back</a>";
    }
}

function add_registration()
{
	$name =$_POST['name'];
	$gender = $_POST['gender'];
	$password = $_POST['password'];
	$password2 = $_POST['password2'];
	$email = $_POST['email'];
	$hp = $_POST['hp'];
	$ic = $_POST['ic'];
	$type = $_POST['type'];
	$User = new User();

        if (!isset($name) || strlen($name) < 6 || strlen($password) < 6 || $password != $password2 ||
!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) || strlen($hp) < 8 || $type!=2  && $type!=3) {
		echo " <h2>Ooops, errors were encountered, these are the possible solutions:</h2>
		<table>
		<tr><td>User name must have at least 6 letters.</td></tr>
		<tr><td>Password must have at least 6 letters.</td></tr>
		<tr><td>Confirm Password must be same as Password.</td></tr>
		<tr><td>Please follow correct format for email (<i>Example: abc@def.com</i>)</td></tr>
		<tr><td>8 numbers for phone numbers</td></tr>	
		<tr><td>9 letters for IC</td></tr>
		<tr><td>User must be register as a Landlord or Tenants</td></tr>
		</table><br>
        <a href='register.php'>Click here</a> to register again. <br>
        <a href='index.php'>Back</a>";
	} else {
		$result=$User->add_user($name, sha1($password), $email, $ic, $hp, $type, $gender);
	}
}

function get_member_id($id, $type)
{
	$user = new User();
	if($type==2)
	{
		$member_id = $user->landlord_get_member_id($id);
	}
	else if($type==3)
	{
		$member_id = $user->tenant_get_member_id($id);
	}
        else if($type==1)
	{
		$member_id = $user->admin_get_member_id($id);
	}
	return $member_id;
}

function view_profile($member_id)
{
	$user = new User();
	$profile = $user->get_member_info($member_id);
	foreach ($profile as $one_profile)
	{
		$member_list = "<img src='image/banners/viewprofile.png'><br><br>";
		$member_list .= "<table border=1 cellspacing=0>";
		$member_list .= "<tr><th width=110>Member ID</th><td width=250>".$one_profile['member_id']. "</td></tr>";
		$member_list .= "<tr><th>Member Name</th><td>".$one_profile['member_name']. "</td></tr>";
		$member_list .= "<tr><th>Member email</th><td>".$one_profile['member_email']. "</td></tr>";
		$member_list .= "<tr><th>Member IC</th><td>".$one_profile['member_ic']. "</td></tr>";
		$member_list .= "<tr><th>Member HP</th><td>".$one_profile['member_hp']. "</td></tr>";
		$member_list .= "<tr><th>Member Score</th><td>".$one_profile['member_score']. "</td></tr>";
		$member_list .= "<tr><th>Time Registered</th><td>".$one_profile['register_time']. "</td></tr>";
		$member_list .= "</table><br>";
                $member_list .= "<a href='index.php'>Back to Home</a><br>";
                $member_list .= "<a href='javascript:history.go(-1)'>Back to Last Page</a>";
	}
	
	return $member_list;
}

function edit_member_form() 
{
	$member_id = $_SESSION['member_id'];
	if (isset($member_id)) {
		$user2 = new User();
		$all_member = $user2->get_member_info($member_id);
		foreach ($all_member as $one_member) {
		$form = "<img src='image/banners/editprofile.png'><br><br>";
		$form .= "<form name='editForm' method = 'post' action = 'do_editprofile.php' dojoType='dijit.form.Form' execute='submitIfValid(this.isValid())'>";
		$form .= "<table>";
		$form .= "<tr><td>Member ID:</td><td><b>" .$member_id. "</b></td></tr>";
		$form .= "<tr><td>Name:</td><td><input type = 'text' name = 'name' value='" .$one_member['member_name']. "' dojotype='dijit.form.ValidationTextBox' required='true' promptmessage= 'Please click Check Name to fill in user name'></td></tr>";
		$form .= "<tr><td>Old Password:</td><td><input type = 'password' name = 'password0' dojotype='dijit.form.ValidationTextBox' minlength='6'></td></tr>";
		$form .= "<tr><td>New Password:</td><td><input type = 'password' name = 'password1' dojotype='dijit.form.ValidationTextBox' minlength='6'></td></tr>";
		$form .= "<tr><td>Confirm New Password:</td><td><input type = 'password' name = 'password2' dojotype='dijit.form.ValidationTextBox' minlength='6'></td></tr>";
		$form .= "<tr><td>If you don't wish to change your password, please ignore the password section.</td></tr>";
                $form .= "<tr><td>Gender:</td><td><select name='gender' id='gender' dojoType='dijit.form.FilteringSelect'
                         autocomplete='false'>
                         <option value='M'>Male</option>
                         <option value='F'>Female</option>
                         </select></td>";
                $form .= "<tr><td>IC:</td><td><input type = 'text' name = 'ic' value='" .$one_member['member_ic']. "' dojoType='dijit.form.ValidationTextBox' maxlength='9' minlength='9' required='true' invalidMessage='Invalid IC.'></td></tr>";
		$form .= "<tr><td>Email:</td><td><input type = 'text' name = 'email' value='" .$one_member['member_email']. "' dojoType='dijit.form.ValidationTextBox' regExp='[\w]+@[\w]+.[\w]+' required='true' invalidMessage='Wrong Email format'></td></tr>";
		$form .= "<tr><td>Handphone No:</td><td><input type = 'text' name = 'hp' value='" .$one_member['member_hp']. "' dojoType='dijit.form.ValidationTextBox' regExp='\d{8}' required='true' invalidMessage='Invalid Phone number.'></td></tr>";
		$form .= "<tr><td><br></td><td><input type = 'submit' value = 'Submit'></td></tr></table></form><br>";
		return $form;
		}
	}
	else {
		return "Please Log in first<br><a href='index.php'>Back to Home</a>";
	}
	
}

function do_check_valid_user() {
	$name = $_POST['name'];
	if (strlen($name)<6) {
	 return "Invalid user name. Minimum 6 letters are required in Name column.<br><br><a href='check_valid_user.php'>Try Again</a>";	
	} else {
		$user = new User();	
		$record = $user->get_member_rows($name);
		if ($record >= 1 ) {
			return "The name is used, please try again.<br><br><a href='check_valid_user.php'>Try Again</a>";
		} else {
			return "Congratulations! The name is valid. <br><br><a href='javascript:window.close();' 	 																	onclick=opener.location='register.php?name=" .$name. "'>Click Here</a> to use the user name";
		}
	}
}

function do_edit_member() {
	$user3 = new User();
	$member_id = $_SESSION['member_id'];
	$name =$_POST['name'];
	$email = $_POST['email'];
	$hp = $_POST['hp'];
	$ic = $_POST['ic'];
        $gender = $_POST['gender'];
	if ($_POST['password1']!=""){
		if (strlen($_POST['password1'])<6 || sha1($_POST['password0'])!=$_SESSION['member_password'] || $_POST['password1']!=$_POST['password2']) {
			echo "<b>Edit failed</b><br><br>";
			echo "Possible reasons: <br><ul>";
			echo "<li>Password length must be at least 6 words!!</li><br>";
			echo "<li>The old password is wrong!</li><br>";
			echo "<li>New Password and Confirm Password are not matched</li><br></ul>";
			echo "<a href='edit_profile.php'>Edit again</a><br><a href='index.php'>Back</a>";
		} else {
			$password = sha1($_POST['password1']);
			$result= $user3->update_member($member_id, $name, $password, $email, $ic, $hp, $gender);
		}
	} else {
		$password = $_SESSION['member_password'];
		$result= $user3->update_member($member_id, $name, $password, $email, $ic, $hp, $gender);
	}
}

function login_form() {
	
	$form .= "<form method = 'post' action = 'do_login.php'>";
	$form .= "<table><tr><td>Username</td><td><input type='text' name='username' size=15></td></tr>";
	$form .= "<tr><td>Password</td><td><input type='password' name='password' size=15></td></tr>";
	$form .= "<tr><td><center><input name='button' type='submit' value='Login'></center></td></tr></table>";
	$form .= "</form><br>";
	$form .= "<p><a href='forget_password.php'>Forget Password</a></p>";
	
	return $form;
}

function check_member_type(){

	$username = mysql_escape_string(htmlspecialchars($_POST['username']));
	$password = sha1($_POST['password']);
	$user = new User();
	$user_name = $user->get_user_name($username,$password);
	$user_type = $user->get_member_type($username,$password);
	$user_id = $user->get_member_id($username,$password);
	
	$member_password = $user->get_member_password($username, $password);
	if($user_name){
	
		$_SESSION['username'] = $username;
		$_SESSION['member_type'] = $user_type;
		$_SESSION['member_id'] = $user_id;
		$_SESSION['member_password'] = $member_password;
		
		
		if($user_type==1) {
			return "<div id=\"box1\"><div id=\"box\"><br>
				Welcome back, " .$_SESSION['username']. ".<br>
				You are logged in as Administrator.<br><br>
				The webpage will be redirected to homepage in 5 seconds, if not, please click <a href='index.php'>HERE</a></div></div>
			<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
			
		else if($user_type==2) {
		$type_id = $user->get_landlord_id($user_id);
		$_SESSION['type_id'] = $type_id;
			return "<div id=\"box1\"><div id=\"box\"><br>
				Welcome back, " .$_SESSION['username']. ".<br>
				You are logged in as Landlord.<br><br>
				The webpage will be redirected to homepage in 5 seconds, if not, please click <a href='index.php'>HERE</a></div></div>
			<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
		else if($user_type==3) {
		$type_id = $user->get_tenant_id($user_id);
		$_SESSION['type_id'] = $type_id;
			return "<div id=\"box1\"><div id=\"box\"><br>
				Welcome back, " .$_SESSION['username']. ".<br>
				You are logged in as Tenant.<br><br>
				The webpage will be redirected to homepage in 5 seconds, if not, please click <a href='index.php'>HERE</a></div></div>
			<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
	} else{
		return "<div id=\"box1\"><div id=\"box\"><br>
			Login Failed.<br><br>
			The webpage will be redirected to homepage in 5 seconds, if not, please click <a href='login.php'>HERE</a> to Login again</div></div>
			<meta http-equiv='refresh' content='5; URL=login.php'>";
	}
}

function forget_password(){
	$form .= "<img src='image/banners/forgetpassword.png'><br><br>";
	$form .= "Complete the form to receive your new password via email.<br><br>";
	$form .= "<form action='do_forget_password.php' method='post'>";
	$form .= "<table>";
	$form .= "<tr><td>Username</td><td><input type='text' name='username' /></td></tr>";
	$form .= "<tr><td>Email</td><td><input type='text' name='email' /></td></tr>";
	$form .= "<tr><td colspan='2'><input type='submit' /></td></tr>";
	$form .= "</table></form>";
	
	return $form;
}

function createRandomPassword() {
    $chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz023456789";
    $i = 0;
    $pass = '' ;

    while ($i <= 8) {
        $num = mt_rand(0,58);
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return ($pass);
}

function do_forget_password() {
	$username=$_POST['username'];
	$to=$_POST['email'];
	$password = createRandomPassword();
	$encryptpass = sha1($password);
	$subject = "Your new password";
	$header= "From: Student_Housing_Portal@hotmail.com"; 
	$message =  "\nUsername:\t" . $username. 
			"\nNew Password:\t" . $password;
			
	$user= new User();
	$user->update_password($username, $encryptpass);
	
	if ($username) {
		mail($to, $subject, $message, $header); 

		if (mail($to, $subject, $message, $header)) {
 			echo "<p>Mail sent successfully.</p><br><a href='index.php'>Home</a>";
		} else {
 			echo "<p>Mail could not be sent.</p><br><a href='index.php'>Home</a>";
		} 
		echo "<a href='index.php'>Home</a>";
	}
	else {
		echo "Invalid Selection<br><br><a href='index.php'>Home</a>";
	}
}

function logout() 
{
	$username=$_SESSION['username'];
	if (isset( $username )){ //session variable was set: user logged-in.  display goodbye message	
		//destroy the session
		session_destroy();
		echo "<div id=\"box1\"><div id=\"box\"><br>
		Goodbye <i>".$username."</i>.<br><br>
		The webpage will be redirected to homepage in 5 seconds, if not, please click <a href='index.php'>HERE</a></div></div>
		<meta http-equiv='refresh' content='5; URL=index.php'>";
	}
	else {
		echo "<div id=\"box1\"><div id=\"box\"><br>
		You are already logged out.<br><br>
		The webpage will be redirected to homepage in 5 seconds, if not, please click <a href='index.php'>HERE</a></div></div>
		<meta http-equiv='refresh' content='5; URL=index.php'>";
	}
}

function view_member() {
                $type = $_GET['type'];
		$user = new User();
		if ($_SESSION['member_type']==1) {
                    if ($type=="landlord") {
                        $all_member=$user->get_landlord_info($type);
                    }
                    else if ($type=="tenant") {
                        $all_member=$user->get_tenant_info($type);
                    } else {
                        $all_member=$user->get_all_member_info();
                    }
                    $member_list = "<img src='image/banners/deletevacancy.png'><br><br>";
                    $member_list .= "<form method = 'post' action = 'do_delete_member.php' enctype='multipart/form-data' >";
                    $member_list .= "<table border=1><tr><td><b>Select</b></td><td><b>Edit</b></td><td><b>ID</b></td><td><b>Name</b></td>
                                    <td><b>Gender</b></td><td><b>Email</b></td><td><b>Handphone No.</b></td>
                                    <td><b>Score</b></td><td><b>Type</b></td><td><b>Time Registered</b></td></tr>";
                    foreach ($all_member as $one_member) {
			$member_list .= "<tr><td><input type = 'checkbox' name = 'id[]' value='" .$one_member['member_id']. "'></td>";
                        $member_list .= "<td><a href='edit_profile.php?member=" .$one_member['member_id']. "'>Edit</a></td>";
			$member_list .= "<td>".$one_member['member_id']. "</td>";
			$member_list .= "<td>".$one_member['member_name']. "</td>";
			$member_list .= "<td>".$one_member['member_gender']. "</td>";
			$member_list .= "<td>".$one_member['member_email']. "</td>";
                        $member_list .= "<td>".$one_member['member_hp']. "</td>";
                        $member_list .= "<td>".$one_member['member_score']. "</td>";
                        if ($one_member['member_type']==2){
                            $member_list .= "<td>Landlord</td>";
                        }
                        else if ($one_member['member_type']==3) {
                            $member_list .= "<td>Tenant</td>";
                        }
			
			$member_list .= "<td>".$one_member['register_time']. "</td>";
			$member_list .= "</tr>";

			}
			$member_list .= "</table>";
			$member_list .= "<input type='submit' value='Delete'></form>";
                    return $member_list;
		} else {
			return "Please Log in as Admin first. <br><br><a href='index.php'>";
		}
}

function admin_edit_member_form()
{
	$member_id = $_GET['member'];
	if (isset($member_id)) {
		$user2 = new User();
		$all_member = $user2->get_member_info($member_id);
		foreach ($all_member as $one_member) {
		$form = "<img src='image/banners/editprofile.png'><br><br>";
		$form .= "<form name='editForm' method = 'post' action = 'do_editprofile.php' dojoType='dijit.form.Form' execute='submitIfValid(this.isValid())'>";
		$form .= "<table>";
		$form .= "<tr><td>Member ID:</td><td><b>" .$member_id. "</b></td></tr>";
		$form .= "<tr><td>Name:</td><td><input type = 'text' name = 'name' value='" .$one_member['member_name']. "' dojotype='dijit.form.ValidationTextBox' required='true' promptmessage= 'Please click Check Name to fill in user name'></td></tr>";
		$form .= "<tr><td>Old Password:</td><td><input type = 'password' name = 'password0' dojotype='dijit.form.ValidationTextBox' minlength='6'></td></tr>";
		$form .= "<tr><td>New Password:</td><td><input type = 'password' name = 'password1' dojotype='dijit.form.ValidationTextBox' minlength='6'></td></tr>";
		$form .= "<tr><td>Confirm New Password:</td><td><input type = 'password' name = 'password2' dojotype='dijit.form.ValidationTextBox' minlength='6'></td></tr>";
		$form .= "<tr><td>If you don't wish to change your password, please ignore the password section.</td></tr>";
                $form .= "<tr><td>Gender:</td><td><select name='gender' id='gender' dojoType='dijit.form.FilteringSelect'
                         autocomplete='false'>
                         <option value='M'>Male</option>
                         <option value='F'>Female</option>
                         </select></td>";
                $form .= "<tr><td>IC:</td><td><input type = 'text' name = 'ic' value='" .$one_member['member_ic']. "' dojoType='dijit.form.ValidationTextBox' maxlength='9' minlength='9' required='true' invalidMessage='Invalid IC.'></td></tr>";
		$form .= "<tr><td>Email:</td><td><input type = 'text' name = 'email' value='" .$one_member['member_email']. "' dojoType='dijit.form.ValidationTextBox' regExp='[\w]+@[\w]+.[\w]+' required='true' invalidMessage='Wrong Email format'></td></tr>";
		$form .= "<tr><td>Handphone No:</td><td><input type = 'text' name = 'hp' value='" .$one_member['member_hp']. "' dojoType='dijit.form.ValidationTextBox' regExp='\d{8}' required='true' invalidMessage='Invalid Phone number.'></td></tr>";
		$form .= "<tr><td><br></td><td><input type = 'submit' value = 'Submit'></td></tr></table></form><br>";
		return $form;
		}
	}
	else {
		return "Invalid Selection<br><a href='index.php'>Back to Home</a>";
	}

}

function do_delete_member() {
	$user = new User();
        $vacancy = new Vacancy();
	$id=$_POST['id'];
        $count=0;
        if ($_SESSION['member_type']==1){
            if ($id==null) {
                return "No member(s) are selected<br><a href='index.php'>Back</a>";
            } else {
                foreach ($id as $member_id) {
                    $type = $user->get_type($member_id);
                    if ($type==2) {
                        $landlord_id = $user->get_landlord_id($member_id);
                        $result =  $user->delete_landlord($member_id);
                        $result2 = $vacancy->delete_member_vacancy($landlord_id);
                    }
                    else if ($type==3) {
                        $result = $user->delete_tenant($member_id);
                    }
                    $result = $user->delete_member($member_id);
                    $count++;
                    if ($result) {
                        return $count. " Member(s) have been deleted<br>
                               The related vacancies were deleted too.<br><br><a href='index.php'>Back</a>";
                    }
                }
            }
        } else {
            return "Please log in as Admin first <br><br><href='index.php'>Back</a>";
        }
	
}
//<--

//-->Functions for vacancy
function vacancy_form() {
	
	$username=$_SESSION['username'];
	$usertype=$_SESSION['member_type'];
	$member_id=$_SESSION['member_id'];
	
	if (isset($username) && $usertype==2) {
	
	$User = new User();
	$landlord_id=$User->get_landlord_id($member_id);
	
		$form .= "<img src='image/banners/addvacancy.png'><br><br>";
		$form .= "<form 'myform' dojoType='dijit.form.Form' method = 'post' action = 'do_add_vacancy.php' enctype='multipart/form-data' execute='submitIfValid(this.isValid())'>";
		$form .= "<table>";
		$form .= "<tr><td>Landlord ID: </td><td><b>" .$landlord_id. "</b></td></tr>";
		$form .= "<tr><td>Landlord Name: </td><td><b>" .$username. "</b></td></tr>";
		$form .= "<tr><td>Title:</td><td><input type = 'text' name = 'title' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                $form .= "<tr><td>House Type:</td><td><select name='type' id='type' dojoType='dijit.form.FilteringSelect' autocomplete='false'>
                  <option value='HDB'>HDB</option>
                  <option value='Private Apartment'>Private Apartment</option>
                  <option value='Landed Property'>Landed Property</option>
                  </select></td></tr>";
                $form .= "<tr><td>Rooms:</td><td><input type = 'text' name = 'room' dojotype='dijit.form.ValidationTextBox' required='true' maxlength=1></td></tr>";
                $form .= "<tr><td>Rental:</td><td><input type = 'text' name = 'rental' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                $form .= "<tr><td>Unit:</td><td><input type = 'text' name = 'unit' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                $form .= "<tr><td>Block:</td><td><input type = 'text' name = 'block' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
		$form .= "<tr><td>Address:</td><td><input type = 'text' name = 'address' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
		$form .= "<tr><td>Postal Code:</td><td><input type = 'text' name = 'postal' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
		$form .= "<tr><td>Min. tenants required:</td><td><input type = 'text' name = 'sharing' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                $form .= "<tr><td>Description:</td><td><input type = 'textarea' name = 'description' dojotype='dijit.form.Textarea' style='width:200px' style='height:300px'></td></tr>";
		$form .= "<tr><td>Photo:</td><td><input type = 'file' name = 'image'></td></tr>";
		$form .= "<tr><td></td><td><input type = 'submit' value = 'Submit'></td></tr></table></form><br>";
                $form .= "<i>Please fill in all of the columns</i>";
	
		return $form;
	}
	else {
	return "You are not Landlord, Please log in as landlord first.<br><a href='index.php'>Back to Homepage</a>";
	}
}

function upload_image(){
	/*
	*Write description here
	allows members to upload images
	*/
	$target_path = "image/";
	$base_name = basename($_FILES['image']['name']);
	$target_path = $target_path . $base_name;
	if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
		return true;
	}else{
		return false;
	}
}

function add_vacancy(){
	/*
	*Write description here
	Add the information of the product into database
	*/
	$username=$_SESSION['username'];
	$usertype=$_SESSION['member_type'];
	$member_id=$_SESSION['member_id'];
	if (isset($username) && $usertype==2){
	
            $User = new User();
            $landlord_id=$User->get_landlord_id($_SESSION['member_id']);
            $Vacancy = new Vacancy();
            $title = $_POST['title'];
            $type = $_POST['type'];
            $room = $_POST['room'];
            $rental = $_POST['rental'];
            $unit = $_POST['unit'];
            $block = $_POST['block'];
            $address = $_POST['address'];
            $postal_id = $_POST['postal'];
            $sharing = $_POST['sharing'];
            $description =$_POST['description'];
            $location_id = $Vacancy->set_location_id($postal_id);
            $is_uploaded = upload_image($_FILES);


		if(is_uploaded)
		{
			$image = basename($_FILES['image']['name']);
			$vacancy_id = $Vacancy->add_vacancy($landlord_id, $location_id, $title, $type, $room, $rental, $unit, $block, $address,
                            $postal_id, $sharing, $description, $image);
			return "Your new vacancy is added successfully! <br>
		    	The webpage will be redirected to homepage in 5 seconds, if not, please click
				<a href='index.php'>HERE</a>
				<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
		else{
			$image = "";
			$vacancy_id = $Vacancy->add_vacancy($landlord_id, $location_id, $title, $type, $room, $rental, $unit, $block, $address,
                            $postal_id, $sharing, $description, $image);
			return "Your new vacancy is added successfully! <br>
		    	The webpage will be redirected to homepage in 5 seconds, if not, please click
				<a href='index.php'>HERE</a>
				<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
	}
}

function get_address() {
	$Vacancy2 = new Vacancy();
	$vacancy_id =$_GET['vacancy_id'];
	echo $Vacancy2->get_address($vacancy_id);
}	

function view_vacancy() {

	$user = new User();
	$landlord_id= $user->get_landlord_id($_SESSION['member_id']);
	$Vacancy = new Vacancy();
	$all_vacancy=$Vacancy->list_all_vacancy();
	$vacancy_list .= "<img src='image/banners/viewvacancy.png'><br><br>";
            $vacancy_list .= "<table border=1 width='550' cellspacing='0'><tr bgcolor=#BFBFBF><td width='70'><center><b>Vacancy ID*</b></center></td>
		<td width='110'><center><b>Title</b></center></td><td width='75'><center><b>Type</b></center></td><td width='145'><center>
		<b>Address</b></center></td><td width='60'><center><b>Rental</b></center></td><td width='60'><center><b>Available</b></center></td>
		<td width='90'><center><b>Time Added</b></center></td></tr>";
            foreach ($all_vacancy as $one_vacancy) {
		$vacancy_list .= "<tr><td><center><a href=view_vacancy_detail.php?vacancy_id=" .$one_vacancy['vacancy_id']. ">".$one_vacancy['vacancy_id']. "</a></center></td>";
		$vacancy_list .= "<td>".$one_vacancy['vacancy_title']. "</td>";
		$vacancy_list .= "<td>".$one_vacancy['vacancy_type']. "</td>";
		$vacancy_list .= "<td>Unit ".$one_vacancy['vacancy_unit'].", Blk ".$one_vacancy['vacancy_block'].", "
			.$one_vacancy['vacancy_address'].", ".$one_vacancy['postal_id']."</td>";
		$vacancy_list .= "<td>SGD " .$one_vacancy['vacancy_rental']. "</td>";
		if ($one_vacancy['vacancy_available']==1) {
			$vacancy_list .= "<td><center>YES</center></td>";
		} else {
			$vacancy_list .= "<td><center>NO</center></td>";
		}
		$vacancy_list .= "<td>".$one_vacancy['add_time']. "</td>";
		$vacancy_list .= "</tr>";
            }
        
        $vacancy_list .= "</table><br>";
            $vacancy_list .= "<p><i>*Click on the Vacancy ID to see the details</i></p>";
	return $vacancy_list;
}

function admin_view_vacancy() {
	
	$user = new User();
	$landlord_id= $user->get_landlord_id($_SESSION['member_id']);
	$Vacancy = new Vacancy();
	if ($landlord_id) {
		$all_vacancy=$Vacancy->list_editable_vacancy($landlord_id);
		$landlord_name = $user->get_landlord_name($landlord_id);
		$vacancy_list = "<img src='image/banners/deletevacancy.png'><br><br>";
		$vacancy_list .= "<form method = 'post' action = 'do_delete_vacancy.php' enctype='multipart/form-data' >";
		$vacancy_list .= "<table border=1 width=550 cellspacing=0><tr bgcolor=#BFBFBF><th width=50><center>Select</center></th><th colspan=4><center>Information</center>	</th></tr>";
		foreach ($all_vacancy as $one_vacancy) {
			$vacancy_list .= "<tr><td rowspan='3'><center><input type='checkbox' name='id[]' value='" .$one_vacancy['vacancy_id']. "'></center></td>";
			$vacancy_list .= "<th width=90>Vacancy ID</th><td>".$one_vacancy['vacancy_id']."</td><th width=90>Owner Name</th><td><b>".$landlord_name."</b></td></tr>";
			$vacancy_list .= "<tr><th>Title</th><td>".$one_vacancy['vacancy_title']. "</td><th>Type</th><td>type</td></tr>";
			$vacancy_list .= "<tr><th>Address</th><td>".$one_vacancy['vacancy_address']."</td><th>Time Added</th><td>".$one_vacancy['add_time']."</td></tr>";
		}
		$vacancy_list .= "</table>";
		$vacancy_list .= "<input type='submit' value='Delete'></form><br>";
		return $vacancy_list;
	}
        else if ($_SESSION['member_type']==1) {
            $all_vacancy=$Vacancy->list_all_vacancy();
            $vacancy_list = "<img src='images/banners/deletevacancy.png'><br><br>";
            $vacancy_list .= "<form method = 'post' action = 'do_delete_vacancy.php' enctype='multipart/form-data' >";
            $vacancy_list .= "<table border=1 width='550' cellspacing='0'><tr bgcolor=#BFBFBF><td><b>Select</b></td><td><b>Edit</b></td>
            <td width='70'><center><b>Vacancy ID*</b></center></td><td width='110'><center><b>Title</b></center></td>
            <td width='75'><center><b>Type</b></center></td><td width='145'><center><b>Address</b></center></td>
            <td width='60'><center><b>Available</b></center></td>
            <td width='90'><center><b>Time Added</b></center></td></tr>";
            foreach ($all_vacancy as $one_vacancy) {
            	$vacancy_list .= "<tr><td><center><input type = 'checkbox' name = 'id[]' value='" .$one_vacancy['vacancy_id']. "'></center></td>";
                $vacancy_list .= "<td><a href='edit_vacancy.php?vacancy=" .$one_vacancy['vacancy_id']. "&landlord=".$one_vacancy['landlord_id']."'>Edit</a></td>";
            	$vacancy_list .= "<td><center><a href='view_vacancy_detail.php?vacancy_id=" .$one_vacancy['vacancy_id']. "'>".$one_vacancy['vacancy_id']. "</a></center></td>";
		$vacancy_list .= "<td>".$one_vacancy['vacancy_title']. "</td>";
		$vacancy_list .= "<td>".$one_vacancy['vacancy_type']. "</td>";
		$vacancy_list .= "<td>Unit ".$one_vacancy['vacancy_unit'].", Blk ".$one_vacancy['vacancy_block'].", "
			.$one_vacancy['vacancy_address'].", ".$one_vacancy['postal_id']."</td>";
		if ($one_vacancy['vacancy_available']==1) {
			$vacancy_list .= "<td><center>YES</center></td>";
		} else {
			$vacancy_list .= "<td><center>NO</center></td>";
		}
		$vacancy_list .= "<td>".$one_vacancy['add_time']. "</td>";
		$vacancy_list .= "</tr>";
	}
	$vacancy_list .= "</table><br>";
	$vacancy_list .= "<p><i>*Click on the Vacancy ID to see the details</i></p>";
        $vacancy_list .= "<input type=button value='Delete' name='delete'>";
	return $vacancy_list;
       } else {
            return "Please Log in as Administrator first. <br><a href='index.php'>Back</a>";
	}
}

function do_delete_vacancy() 
{

	$vacancy = new Vacancy();
	$id=$_POST['id'];
	$count=0;
	foreach ($id as $idname)
	{	
		$result = $vacancy->delete_vacancy($idname);
		$count++;
	}
	if ($result) {
		return $count. " vacancies have been deleted<br><a href='index.php'>Back</a>";
	}
	else {
		return "Delete Failed<br><a href='index.php'>Back</a>";
	}
}

function do_delete_message() 
{

	$vacancy = new Vacancy();
	$id=$_POST['id'];
	$count=0;
	foreach ($id as $idname)
	{	
		$result = $vacancy->delete_message($idname);
		$count++;
	}
	if ($result) {
		return $count. " Messages have been deleted<br>
		<a href='index.php'>Back to Home</a><br>
		<a href='javascript:history.go(-1)'>Back to Last Page</a>";
	}
	else {
		return "Delete Failed<br><a href='index.php'>Back</a>";
	}
}

function view_editable_vacancy() 
{

	if ($_SESSION['member_type']==2) {
		$user = new User();
		$landlord_id= $user->get_landlord_id($_SESSION['member_id']);
		$Vacancy = new Vacancy();
		$all_vacancy=$Vacancy->list_editable_vacancy($landlord_id);
		$vacancy_list = "<img src='image/banners/viewvacancy.png'><br><br>";
		$vacancy_list .= "<table border=1 width='550' cellspacing='0'><tr bgcolor=#BFBFBF><td colspan='2'><center><b>Vacancy ID</b></center></td><td width='110'><center><b>Title</b></center></td><td width='75'><center><b>Type</b></center></td><td width='145'><center><b>Address</b></center></td><td width='60'><center><b>Available</b></center></td><td width='90'><center><b>Time Added</b></center></td></tr>";
		foreach ($all_vacancy as $one_vacancy) {
			$vacancy_list .= "<tr><td width='30'><center><a href=view_vacancy_detail.php?vacancy_id=" .$one_vacancy['vacancy_id']. ">".$one_vacancy['vacancy_id']. "</a></center></td>";
			$vacancy_list .= "<td width='35'><center><a href=edit_vacancy.php?vacancy_id=" .$one_vacancy['vacancy_id']. ">Edit</a></center></td>";
			$vacancy_list .= "<td>".$one_vacancy['vacancy_title']. "</td>";
			$vacancy_list .= "<td>Apartment</td>";
			$vacancy_list .= "<td>".$one_vacancy['vacancy_address']. "</td>";
			if ($one_vacancy['vacancy_available'] = 1){
				$vacancy_list .= "<td>Yes</td>";
			} else {
				$vacancy_list .= "<td>No</td>";
			}
			$vacancy_list .= "<td>".$one_vacancy['add_time']. "</td>";
			$vacancy_list .= "</tr>";
		}
		$vacancy_list .= "</table><br>";
		return $vacancy_list;
	}
	else 
	{
		return "You are not landlord, you cannot edit vacancy.<br><a href='index.php'>back</a>";
	}	
}

function edit_vacancy_form() 
{
	$username=$_SESSION['username'];
	$usertype=$_SESSION['member_type'];
	$member_id=$_SESSION['member_id'];
	$vacancy_id=$_GET['vacancy_id'];	
	$vacancy=new Vacancy();
        $User = new User();
	if (isset($username) && $usertype==2) {
		$landlord_id=$User->get_landlord_id($member_id);
		$all_vacancy = $vacancy->list_edit_vacancy($vacancy_id);
		foreach ($all_vacancy as $one_vacancy) {
			$form .= "<img src='image/banners/editvacancy.png'><br><br>";
			$form .= "<form 'myform' method = 'post' action = 'do_edit_vacancy.php?vacancy_id=" .$vacancy_id. "' enctype='multipart/form-data'>";
			$form .= "<table>";
			$form .= "<tr><td width=100>Landlord ID: </td><td><b>" .$landlord_id. "</b></td></tr>";
			$form .= "<tr><td>Landlord Name: </td><td><b>" .$username. "</b></td></tr>";
			$form .= "<tr><td>Vacancy ID: </td><td><b>" .$vacancy_id. "</b></td></tr>";
			$form .= "<tr><td>Title:</td><td><input type = 'text' name = 'title' value='" .$one_vacancy['vacancy_title']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>House Type:</td><td><select name='type' id='type' dojoType='dijit.form.FilteringSelect' autocomplete='false'>
                  <option value='HDB'>HDB</option>
                  <option value='Private Apartment'>Private Apartment</option>
                  <option value='Landed Property'>Landed Property</option>
                  </select></td></tr>";
                        $form .= "<tr><td>Rooms:</td><td><input type = 'text' name = 'room' value='" .$one_vacancy['vacancy_room']. "' dojotype='dijit.form.ValidationTextBox' required='true' maxlength=1></td></tr>";
                        $form .= "<tr><td>Rental:</td><td><input type = 'text' name = 'rental' value='" .$one_vacancy['vacancy_rental']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Unit:</td><td><input type = 'text' name = 'unit' value='" .$one_vacancy['vacancy_unit']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Block:</td><td><input type = 'text' name = 'block' value='" .$one_vacancy['vacancy_block']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Address:</td><td><input type = 'text' name = 'address' value='" .$one_vacancy['vacancy_address']. "' value='" .$one_vacancy['vacancy_address']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>Postal Code:</td><td><input type = 'text' name = 'postal' value='" .$one_vacancy['postal_id']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>Min. tenants required:</td><td><input type = 'text' name = 'sharing' value='" .$one_vacancy['vacancy_sharing']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Description:</td><td><input type = 'textarea' name = 'description' rows='6' cols='40' value='" .$one_vacancy['vacancy_description']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>Photo:</td><td><input type = 'file' name = 'image' value='" .$one_vacancy['vacancy_photo']. "'></td></tr>";
			$form .= "<tr><td></td><td><input type = 'submit' value = 'Submit'></td></tr></table></form><br>	";
                        $form .= "<i>Please fill in all of the columns</i>";
		}

		return $form;
	}
        else if (isset($username) && $usertype==1) {
                $vacancy_id = $_GET['vacancy'];
                $landlord_id = $_GET['landlord'];
		$landlord_id=$User->get_landlord_id($member_id);
		$all_vacancy = $vacancy->list_edit_vacancy($vacancy_id);
		foreach ($all_vacancy as $one_vacancy) {
			$form .= "<img src='image/banners/editvacancy.png'><br><br>";
			$form .= "<form 'myform' method = 'post' action = 'do_edit_vacancy.php?vacancy_id=" .$vacancy_id. "' enctype='multipart/form-data'>";
			$form .= "<table>";
			$form .= "<tr><td width=100>Landlord ID: </td><td><b>" .$landlord_id. "</b></td></tr>";
			$form .= "<tr><td>Landlord Name: </td><td><b>" .$username. "</b></td></tr>";
			$form .= "<tr><td>Vacancy ID: </td><td><b>" .$vacancy_id. "</b></td></tr>";
			$form .= "<tr><td>Title:</td><td><input type = 'text' name = 'title' value='" .$one_vacancy['vacancy_title']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>House Type:</td><td><select name='type' id='type' dojoType='dijit.form.FilteringSelect' autocomplete='false'>
                  <option value='HDB'>HDB</option>
                  <option value='Private Apartment'>Private Apartment</option>
                  <option value='Landed Property'>Landed Property</option>
                  </select></td></tr>";
                        $form .= "<tr><td>Rooms:</td><td><input type = 'text' name = 'room' value='" .$one_vacancy['vacancy_room']. "' dojotype='dijit.form.ValidationTextBox' required='true' maxlength=1></td></tr>";
                        $form .= "<tr><td>Rental:</td><td><input type = 'text' name = 'rental' value='" .$one_vacancy['vacancy_rental']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Unit:</td><td><input type = 'text' name = 'unit' value='" .$one_vacancy['vacancy_unit']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Block:</td><td><input type = 'text' name = 'block' value='" .$one_vacancy['vacancy_block']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Address:</td><td><input type = 'text' name = 'address' value='" .$one_vacancy['vacancy_address']. "' value='" .$one_vacancy['vacancy_address']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>Postal Code:</td><td><input type = 'text' name = 'postal' value='" .$one_vacancy['postal_id']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>Min. tenants required:</td><td><input type = 'text' name = 'sharing' value='" .$one_vacancy['vacancy_sharing']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
                        $form .= "<tr><td>Description:</td><td><input type = 'textarea' name = 'description' rows='6' cols='40' value='" .$one_vacancy['vacancy_description']. "' dojotype='dijit.form.ValidationTextBox' required='true'></td></tr>";
			$form .= "<tr><td>Photo:</td><td><input type = 'file' name = 'image' value='" .$one_vacancy['vacancy_photo']. "'></td></tr>";
			$form .= "<tr><td></td><td><input type = 'submit' value = 'Submit'></td></tr></table></form><br>	";
                        $form .= "<i>Please fill in all of the columns</i>";
		}

		return $form;
	}
	else 
	{
	return "You are not Landlord, Please log in as landlord first.<br><a href='index.php'>Back to Homepage</a>";
	}
}

function do_edit_vacancy(){
	/*
	*Write description here
	Add the information of the product into database
	*/
	$username2=$_SESSION['username'];
	$usertype2=$_SESSION['member_type'];
	$member_id=$_SESSION['member_id'];
	$vacancy_id = $_GET['vacancy_id'];
	
	if (isset($username2) && $usertype2==2){
	
            $title = $_POST['title'];
            $type = $_POST['type'];
            $room = $_POST['room'];
            $rental = $_POST['rental'];
            $unit = $_POST['unit'];
            $block = $_POST['block'];
            $address = $_POST['address'];
            $postal_id = $_POST['postal'];
            $sharing = $_POST['sharing'];
            $description =$_POST['description'];
            $Vacancy = new Vacancy();
            $location_id = $Vacancy->set_location_id($postal_id);
            $is_uploaded = upload_image($_FILES);

		if(is_uploaded){
			$image = basename($_FILES['image']['name']);
			$result = $Vacancy->update_vacancy($landlord_id, $location_id, $title, $type, $room, $rental, $unit, $block, $address,
                            $postal_id, $sharing, $description, $image, $vacancy_id);
			return "Your vacancy has been updated successfully! <br>
		    	The webpage will be redirected to homepage in 5 seconds, if not, please click
				<a href='index.php'>HERE</a>
				<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
		else{
			$image = "";
			$result = $Vacancy->update_vacancy($landlord_id, $location_id, $title, $type, $room, $rental, $unit, $block, $address,
                            $postal_id, $sharing, $description, $image, $vacancy_id);
			return "Your vacancy has been updated successfully! <br>
		    	The webpage will be redirected to homepage in 5 seconds, if not, please click
				<a href='index.php'>HERE</a>
				<meta http-equiv='refresh' content='5; URL=index.php'>";
		}
	}
}

function search_quick() 
{
	$form .= "<img src='image/banners/search.png'><br>";
	$form .= "<form method = 'post' action = 'do_search.php'>";
	$form .= "  <input name='search' type='text'/> <input name='Button' src='image/searchbutton2.png' type='image' value='search'>";
	$form .= "</form><br>";
	$form .= "<p><a href='search.php'>Advance Search</a></p><br>";
	
	return $form;
}

function search_form() {
	
	$form .= "<img src='image/banners/search2.png'><br>";
	$form .= "<form method = 'post' action = 'do_search.php'>";
	$form .= "<table>";
        $form .= "<td>Type your keyword here:</td><tr>";
	$form .= "<td><input type = 'text' name = 'search' dojotype='dijit.form.ValidationTextBox'></td><tr>";
        $form .= "<td><br></td><tr>";
        $form .= "<td>Search according to:</td><tr>";
        $form .= "<td><br></td><tr>";
        $form .= "<td>House Type</td><tr>";
        $form .= "<td><select name='house' id='house' dojoType='dijit.form.FilteringSelect' autocomplete='yes'>
                  <option value='empty'>Leave this, if don't want to search by using this</option>
                  <option value='hdb'>HDB</option>
                  <option value='private'>Private Apartment</option>
                  <option value='landed'>Landed Properties</option>
                  </select></td></tr>";

        $form .= "<td>Rental</td><tr>";
        $form .= "<td><select name='rental' id='rental' dojoType='dijit.form.FilteringSelect' autocomplete='yes'>
                  <option value='null'>Leave this, if don't want to search by using this</option>
                  <option value='price1'>$200 below</option>
                  <option value='price2'>$200-$399</option>
                  <option value='price3'>$400-$599</option>
                  <option value='price4'>$600-$799</option>
                  <option value='price5'>$800-$999</option>
                  <option value='price6'>$1000 above</option>
                  </select></td></tr>";
	$form .= "<td><input type = 'submit' value = 'Search'></td>";
	$form .= "</table></form>";
	
	return $form;
}

function search_result(){
    $search = $_POST['search'];
    $house = $_POST['house'];
    $rental = $_POST['rental'];
    if ($_POST['house']=="" ) {
	$house="empty";
		
    }
    else if ($_POST['rental']==""){
        $rental="null";
    }
        
	$vacancy = new Vacancy();

	if($house=="empty" && $rental=="null" && $search!="") {
		$all_vacancy = $vacancy->search_function($search);
	}
	else if($house=="empty" && $rental!="null" && $search=="") {
		$all_vacancy = $vacancy->search_rental($rental);
	}
	else if($house!="empty" && $rental=="null" && $search=="") {
		$all_vacancy = $vacancy->search_house($house);
	}
	else if($house!="empty" && $rental!="null" && $search!="") {
		$all_vacancy = $vacancy->search_by_all($house, $rental, $search);
	}
	else if($house=="empty" && $rental!="null" && $search!="") {
		$all_vacancy = $vacancy->search_rental_search($rental, $search);
	}
	else if($house!="empty" && $rental=="null" && $search!="") {
		$all_vacancy = $vacancy->search_house_search($house, $search);
	}
	else if($house!="empty" && $rental!="null" && $search=="") {
		$all_vacancy = $vacancy->search_house_rental($house,$rental);
	}
	else {
		$all_vacancy = $vacancy->list_all_vacancy();
	}

        if($all_vacancy) {
		$vacancy_list .= "<img src='image/banners/viewvacancy.png'><br><br>";
           if($_SESSION['member_type']==2 || $_SESSION['member_type']==3) {
               $vacancy_list .= "<table border=1 width='550' cellspacing='0'><tr bgcolor=#BFBFBF><td width='70'><center><b>Vacancy ID*</b></center></td>
		<td width='110'><center><b>Title</b></center></td><td width='75'><center><b>Type</b></center></td><td width='145'><center>
		<b>Address</b></center></td><td width='60'><center><b>Rental</b></center></td><td width='60'><center><b>Available</b></center></td>
		<td width='90'><center><b>Time Added</b></center></td></tr>";
		foreach ($all_vacancy as $one_vacancy) {
			$vacancy_list .= "<tr><td><center><a href=view_vacancy_detail.php?vacancy_id=" .$one_vacancy['vacancy_id']. ">".$one_vacancy['vacancy_id']. "</a></center></td>";
			$vacancy_list .= "<td>".$one_vacancy['vacancy_title']. "</td>";
			$vacancy_list .= "<td>".$one_vacancy['vacancy_type']. "</td>";
			$vacancy_list .= "<td>".$one_vacancy['vacancy_unit'].", ".$one_vacancy['vacancy_block'].", "
			.$one_vacancy['vacancy_address'].", ".$one_vacancy['postal_id']."</td>";
			$vacancy_list .= "<td>".$one_vacancy['vacancy_rental']. "</td>";
			if ($one_vacancy['vacancy_available']==1) {
				$vacancy_list .= "<td><center>YES</center></td>";
			} else {
				$vacancy_list .= "<td><center>NO</center></td>";
			}
			$vacancy_list .= "<td>".$one_vacancy['add_time']. "</td>";
			$vacancy_list .= "</tr>";
		}
		$vacancy_list .= "</table>";
		return $vacancy_list ."<br><a href='search.php'>Search again</a>";
           }
           else if ($_SESSION['member_type']==1) {
               $vacancy_list .= "<form method = 'post' action = 'do_delete_vacancy.php' enctype='multipart/form-data' >";
            $vacancy_list .= "<table border=1 width='550' cellspacing='0'><tr bgcolor=#BFBFBF><td><b>Select</b></td><td><b>Edit</b></td>
            <td width='70'><center><b>Vacancy ID*</b></center></td><td width='110'><center><b>Title</b></center></td>
            <td width='75'><center><b>Type</b></center></td><td width='145'><center><b>Address</b></center></td>
            <td width='60'><center><b>Available</b></center></td>
            <td width='90'><center><b>Time Added</b></center></td></tr>";
            foreach ($all_vacancy as $one_vacancy) {
            	$vacancy_list .= "<tr><td><center><input type = 'checkbox' name = 'id[]' value='" .$one_vacancy['vacancy_id']. "'></center></td>";
                $vacancy_list .= "<td><a href='edit_vacancy.php?vacancy=" .$one_vacancy['vacancy_id']. "&landlord=".$one_vacancy['landlord_id']."'>Edit</a></td>";
            	$vacancy_list .= "<td><center><a href='view_vacancy_detail.php?vacancy_id=" .$one_vacancy['vacancy_id']. "'>".$one_vacancy['vacancy_id']. "</a></center></td>";
		$vacancy_list .= "<td>".$one_vacancy['vacancy_title']. "</td>";
		$vacancy_list .= "<td>".$one_vacancy['vacancy_type']. "</td>";
		$vacancy_list .= "<td>Unit ".$one_vacancy['vacancy_unit'].", Blk ".$one_vacancy['vacancy_block'].", "
			.$one_vacancy['vacancy_address'].", ".$one_vacancy['postal_id']."</td>";
		if ($one_vacancy['vacancy_available']==1) {
			$vacancy_list .= "<td><center>YES</center></td>";
		}
                else {
                    $vacancy_list .= "<td><center>NO</center></td>";
                }
                    $vacancy_list .= "<td>".$one_vacancy['add_time']. "</td>";
                    $vacancy_list .= "</tr>";
            }
                $vacancy_list .= "</table><br>";
                $vacancy_list .= "<p><i>*Click on the Vacancy ID to see the details</i></p>";
                $vacancy_list .= "<input type=button value='Delete' name='delete'>";
		return $vacancy_list ."<br><a href='search.php'>Search again</a>";
           }
		
	} else {
		return "No result found<br><br><a href='index.php'>Back</a><br><a href='search.php'>Search again</a>";
	}
}

function region_map() {
	
	$return_array = array();
	$region_id = $_GET['region'];
	$vacancy = new Vacancy();
	$all_vacancy = $vacancy->get_region_vacancy($region_id);
	$i=0;
	if ($region_id == 1 || $region_id == 2 || $region_id == 3 || $region_id == 4 || $region_id == 5) {
		foreach ($all_vacancy as $one_vacancy) {
			echo "address[" .$i. "]='" .$one_vacancy['vacancy_address']. ", " .$one_vacancy['postal_id']. "'; ";
                        echo "findLocation(address[".$i."]); ";
                        echo "vacancyID[".$i."]='" .$one_vacancy['vacancy_id']. "'; ";
                        echo "landlordID[".$i."]='" .$one_vacancy['landlord_id']. "'; ";
			$i++;
		}
	} else {
		echo "Invalid region. <br><a href='index.php'>back</a>";	
	}
}

function get_aggregate() {
	
	$return_array = array();
	$region_id = $_GET['region'];
	$vacancy = new Vacancy();
	if ($region_id == 1 || $region_id == 2 || $region_id == 3 || $region_id == 4 || $region_id == 5) {
		$aggregate = $vacancy->get_region_aggregate($region_id);
		return $aggregate;
	} else {
		return "Invalid region. <br><a href='index.php'>back</a>";	
	}
}

function get_vacancy_title() {
	$vacancy = new Vacancy();
	$vacancy_id = $_GET['vacancy_id'];
	$vacancy_title = $vacancy->get_title($vacancy_id);
	return $vacancy_title;
}

function view_vacancy_detail() {

	$user = new User();
	$vacancy = new Vacancy();
	$vacancy_id = $_GET['vacancy_id'];
	$one_vacancy = $vacancy->get_vacancy_detail($vacancy_id);
	$landlord_id = $one_vacancy['landlord_id'];
	$landlord_name = $user->get_landlord_name($one_vacancy['landlord_id']);
	$user_type = $_SESSION['member_type'];

	$vacancy_list = "<img src='image/banners/viewvacancy.png'><br><br>";
	$vacancy_list .= "<table border=1 width='550' cellspacing='0'>";
	$vacancy_list .= "<tr><td rowspan=10 colspan=2 ><center><img src='image/".$one_vacancy['vacancy_photo']."' width='200' height='200'></center></th><th width='110' bgcolor=#BFBFBF>Vacancy ID</th><td width='200'>".$one_vacancy['vacancy_id']. "</td></tr>";
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Title</th><td>".$one_vacancy['vacancy_title']."</td></tr>";
	if ($user_type==1 || $user_type==2 || $user_type==3) {
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Owner Name</th><td><a href=view_profile.php?landlord_id=" .$one_vacancy['landlord_id']. "><b>" .$landlord_name. "</b></a></td></tr>";
	} else {
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Owner Name</th><td><b>" .$landlord_name. "</b></td></tr>";
	}
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Type</th><td>".$one_vacancy['vacancy_type']."</td></tr>";
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Room</th><td>".$one_vacancy['vacancy_room']."</td></tr>";
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Rental</th><td>SGD ".$one_vacancy['vacancy_rental']."</td></tr>";
	if ($one_vacancy['vacancy_available']==1){
		$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Available</th><td>YES</td></tr>";
	} else {
		$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Available</th><td>NO</td></tr>";
	}
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Address</th><td>Unit ".$one_vacancy['vacancy_unit'].", Blk ".$one_vacancy['vacancy_block'].", "
			.$one_vacancy['vacancy_address'].", ".$one_vacancy['postal_id']."</td>";
	$vacancy_list .= "<tr><th bgcolor=#BFBFBF>Time Added</th><td>".$one_vacancy['add_time']."</td></tr>";
	$vacancy_list .= "<tr><th rowspan='2'bgcolor=#BFBFBF>Description</th><td rowspan='2'>".$one_vacancy['vacancy_description']."</td></tr>";
	$vacancy_list .= "<tr><th bgcolor=#0080FF><center><a href=view_map.php?vacancy_id=".$one_vacancy['vacancy_id'].">View map</a></center></th>";
	if($user_type==3 && $one_vacancy['vacancy_available']==1){
		$vacancy_list .= "<td><center><a href=request_for_transaction.php?landlord_id=".$landlord_id."&vacancy_id=".$vacancy_id."><b>Interested? Tell him!</b></a></center></td></tr>";
	}
	$vacancy_list .= "</table><br><br>";
	
	$all_message = $vacancy->get_message($vacancy_id);
	$vacancy_list .= "<img src='image/banners/comments.png'><br><br>";
	$vacancy_list .= "<table border=1 width='550' cellspacing='0'>";
	$vacancy_list .= "<tr><th>Vacancy ID</th><th>Author Member ID</th><th>Content</th>";
	$this_vacancy = $vacancy->get_vacancy_detail($vacancy_id);
	$vacancy_owner = $this_vacancy['landlord_id'];
	
	if(($_SESSION['member_type']==1||$_SESSION['member_type']==2) && $vacancy_owner==$_SESSION['type_id'])
	{
	$vacancy_list .= "<form action='delete_message.php?vacancy_id=".$vacancy_id."' method='post'>";
	$vacancy_list .= "<td>Delete?</td></tr>";
		foreach ($all_message as $one_message) 
		{
		$vacancy_list .= "<tr><td><input type = 'checkbox' name = 'id[]' value='" .$one_message['message_id']. "'></td><td>" .$one_message['vacancy_id']. "</td><td>" .$one_message['member_id']. "</td><td>" .$one_message['content']. "</td></tr>";
		}
	$vacancy_list .= "<tr><td><input type='submit' value='Delete'></td></tr></form><br>";
	}
	else
	{
	$vacancy_list .= "</tr>";
		foreach ($all_message as $one_message) 
		{
			$vacancy_list .= "<tr><td>" .$one_message['vacancy_id']. "</td><td>" .$one_message['member_id']. "</td><td>" .$one_message['content']. "</td></tr>";
		}
	}
	$vacancy_list .= "</table><br><br>";
	$vacancy_list .= "<form action='post_message.php?vacancy_id=".$vacancy_id."' method='post'>";
	$vacancy_list .= "<table border=1>";
	$vacancy_list .= "<tr><td><input type='text' name='content' /></td></tr>";
	$vacancy_list .= "<tr><td colspan='2'><input type='submit' /></td></tr>";
	$vacancy_list .= "</table>";
	$vacaycy_list .= "</form>";
	
        $vacancy_list .= "<a href='index.php'>Back to Home</a><br>";
        $vacancy_list .= "<a href='javascript:history.go(-1)'>Back to Last Page</a><br>";

	return $vacancy_list;
}

function post_message()
{
	$vacancy_id = $_GET['vacancy_id'];
	$content = $_POST['content'];
	$member_id = $_SESSION['member_id'];
	$vacancy = new Vacancy();
	if(isset($_SESSION['username']))
	{
	$success = $vacancy->do_post_message($vacancy_id, $member_id, $content);
	}
	
	if($success=="true")
	{
		$vacancy_list = "Message Posted<br>";
		$vacancy_list .= "<a href='index.php'>Back to Home</a><br>";
        $vacancy_list .= "<a href='javascript:history.go(-1)'>Back to Last Page</a>";
	}
	else
	{
		$vacancy_list = "Fail to post message";
		$vacancy_list .= "<a href='index.php'>Back to Home</a><br>";
        $vacancy_list .= "<a href='javascript:history.go(-1)'>Back to Last Page</a>";
	}
	return $vacancy_list;
}
function request_for_transaction()
{
    if($_SESSION['member_type']==3){
        echo "Transaction processing......";
	$vacancy = new Vacancy();
        $user = new User();
	$vacancy_id = $_GET['vacancy_id'];
        $tenant_id = $user->get_tenant_id($_SESSION['member_id']);
	$title = $vacancy->get_title($vacancy_id);
	$landlord_id = $_GET['landlord_id'];
        $email = $user->get_landlord_email($landlord_id);
	$transaction = new Transaction();
	$transaction->request_for_transaction($vacancy_id, $landlord_id, $tenant_id);
	$transaction->mail_to_landlord($vacancy_id,$title,$landlord_id,$email);
    } else {
        echo "You are not allow to start transaction. <br><br><a href='index.php'>Back</a>";
    }
}

function mail_to_tenant($transaction_id,$email)
{
    $transaction = new Transaction();
    $user = new User();
    $one_transaction=$transaction->get_transaction($transaction_id);
    $tenant_id = $one_transaction['tenant_id'];
    $vacancy_id = $one_transaction['vacancy_id'];
    $email = $user->get_tenant_email($tenant_id);
    $transaction->mail_to_tenant($tenant_id,$email);
    return $vacancy_id;
}

function zoom_region()
{
    $region=$_GET['region'];

    if($region==1){
        return "GLatLng(1.429, 103.8149), 12";
    }
    else if($region==2) {
        return "GLatLng(1.2812, 103.831444), 12";
    }
    else if($region==3) {
        return "GLatLng(1.3498, 103.971519), 12";
    }
    else if($region==4) {
        return "GLatLng(1.35598, 103.680382), 12";
    }
    else if($region==5) {
        return "GLatLng(1.378651, 103.814278), 12";
    }
    else {
        return "GLatLng(1.36, 103.825), 11";
    }
}


function view_transaction()
{
	$admin_id = $_GET['admin_id'];
        $tenant_id = $_GET['tenant_id'];
	$landlord_id = $_GET['landlord_id'];
	$user_type = $_SESSION['member_type'];

	if(!isset($_SESSION['member_id']))
	{
	echo "You need to LOG IN first.";
	echo "<form method = 'post' action = 'do_quick_login.php'>";
	echo "<table>";
	echo "<tr><td>Username:</td><td><input type = 'text' name = 'username'></td></tr>";
	echo "<tr><td>Password:</td><td><input type = 'password' name = 'password'></td></tr>";
	echo "<tr><td></td><td><input type = 'submit' value = 'Login'></td></tr></table></form>";
	}
	else if(isset($tenant_id))
	{
	$transaction = new Transaction();
        $type_id = $tenant_id;
	$all_transaction = $transaction->list_transaction(3,$type_id);
		foreach ($all_transaction as $one_transaction) {
		$transaction_list .= "<table border=1>";
		$transaction_list .= "<tr><td>Transaction ID</td><td>".$one_transaction['transaction_id']."</td></tr>";
		$transaction_list .= "<tr><td>Vacancy ID</td><td><a target='_blank' href=view_vacancy_detail.php?vacancy_id=" .$one_transaction['vacancy_id']. "><b>" .$one_transaction['vacancy_id']. "</b></a></td></tr>";
		$transaction_list .= "<tr><td>Tenant ID</td><td><a target='_blank' href=view_profile.php?tenant_id=" .$one_transaction['tenant_id']. "><b>" .$one_transaction['tenant_id']. "</b></a></td></tr>";
			if($one_transaction['transaction_starttime']=='0000-00-00 00:00:00')
			{
                            $one_transaction['transaction_starttime'] = "/";
			}
			if($one_transaction['transaction_endtime']=='0000-00-00 00:00:00')
			{
                            $one_transaction['transaction_endtime'] = "/";
			}

			if($one_transaction['transaction_status']==0)
			{
                            $one_transaction['transaction_status'] = "No Result Yet";
			}
			else if ($one_transaction['transaction_status']==1)
			{
                            $one_transaction['transaction_status'] = "In progress";
			}
			else if($one_transaction['transaction_status']==2)
			{
                            $one_transaction['transaction_status'] = "Success";
			}
			else if($one_transaction['transaction_status']==3)
			{
                            $one_transaction['transaction_status'] = "Fail";
			}
			$transaction_list .= "<tr><td>Transaction Start Time</td><td>".$one_transaction['transaction_starttime']."</td></tr>";
			$transaction_list .= "<tr><td>Transaction End Time</td><td>".$one_transaction['transaction_endtime']."</td></tr>";
			$transaction_list .= "<tr><td>Transaction Result</td><td>".$one_transaction['transaction_status']."</td></tr>";
			if($one_transaction['transaction_status']=="Success"&&$one_transaction['tenant_rate']==0&&$user_type==3)
			{
                            $transaction_list .= "<tr><td>Transaction Rated</td><td><a href=rate_transaction.php?transaction_id=".$one_transaction['transaction_id']."><b>Rate This Transaction</b></a></td></tr>";
			}
			$transaction_list .= "</table>";
			$transaction_list .= "<br>";
		}
	}
        else if(isset($landlord_id))
	{
	$transaction = new Transaction();
        $type_id = $landlord_id;
	$all_transaction = $transaction->list_transaction(2,$type_id);
		foreach ($all_transaction as $one_transaction) {
		$transaction_list .= "<table border=1>";
		$transaction_list .= "<tr><td>Transaction ID</td><td>".$one_transaction['transaction_id']."</td></tr>";
		$transaction_list .= "<tr><td>Vacancy ID</td><td><a target='_blank' href=view_vacancy_detail.php?vacancy_id=" .$one_transaction['vacancy_id']. "><b>" .$one_transaction['vacancy_id']. "</b></a></td></tr>";
		$transaction_list .= "<tr><td>Tenant ID</td><td><a target='_blank' href=view_profile.php?tenant_id=" .$one_transaction['tenant_id']. "><b>" .$one_transaction['tenant_id']. "</b></a></td></tr>";
			if($one_transaction['transaction_starttime']=='0000-00-00 00:00:00')
			{
			$one_transaction['transaction_starttime'] = "/";
			}
			if($one_transaction['transaction_endtime']=='0000-00-00 00:00:00')
			{
			$one_transaction['transaction_endtime'] = "/";
			}

			if($one_transaction['transaction_status']==0)
			{
                            $one_transaction['transaction_status'] = "No Result Yet";
			}
			else if ($one_transaction['transaction_status']==1)
			{
                            $one_transaction['transaction_status'] = "In progress";
			}
			else if($one_transaction['transaction_status']==2)
			{
                            $one_transaction['transaction_status'] = "Success";
			}
			else if($one_transaction['transaction_status']==3)
			{
                            $one_transaction['transaction_status'] = "Fail";
			}
			$transaction_list .= "<tr><td>Transaction Start Time</td><td>".$one_transaction['transaction_starttime']."</td></tr>";
			$transaction_list .= "<tr><td>Transaction End Time</td><td>".$one_transaction['transaction_endtime']."</td></tr>";
			$transaction_list .= "<tr><td>Transaction Result</td><td>".$one_transaction['transaction_status']."</td></tr>";
			if($one_transaction['transaction_status']=="Success"&&$one_transaction['tenant_rate']==0&&$user_type==3)
			{
			$transaction_list .= "<tr><td>Transaction Rated</td><td><a href=rate_transaction.php?transaction_id=".$one_transaction['transaction_id']."><b>Rate This Transaction</b></a></td></tr>";
			}
			$transaction_list .= "</table>";
			$transaction_list .= "<br>";
		}
	}
	else if($_SESSION['member_type']==1)
	{
	$transaction = new Transaction();
        $type_id = $admin_id;
	$all_transaction = $transaction->list_transaction(1,$type_id);
		foreach ($all_transaction as $one_transaction) {
                    $transaction_list .= "<table border=1>";
                    $transaction_list .= "<tr><td>Transaction ID</td><td>".$one_transaction['transaction_id']."</td></tr>";
                    $transaction_list .= "<tr><td>Vacancy ID</td><td><a target='_blank' href=view_vacancy_detail.php?vacancy_id=" .$one_transaction['vacancy_id']. "><b>" .$one_transaction['vacancy_id']. "</b></a></td></tr>";
                    $transaction_list .= "<tr><td>Tenant ID</td><td><a target='_blank' href=view_profile.php?tenant_id=" .$one_transaction['tenant_id']. "><b>" .$one_transaction['tenant_id']. "</b></a></td></tr>";
                    if($one_transaction['transaction_starttime']=='0000-00-00 00:00:00')
                    {
                        $one_transaction['transaction_starttime'] = "/";
                    }
                    if($one_transaction['transaction_endtime']=='0000-00-00 00:00:00')
                    {
                        $one_transaction['transaction_endtime'] = "/";
                    }
                    if($one_transaction['transaction_status']==0)
                    {
                        $one_transaction['transaction_status'] = "No Result Yet";
                    }
                    else if ($one_transaction['transaction_status']==1)
                    {
                        $one_transaction['transaction_status'] = "In progress";
                    }
                    else if($one_transaction['transaction_status']==2)
                    {
                        $one_transaction['transaction_status'] = "Success";
                    }
                    else if($one_transaction['transaction_status']==3)
                    {
                        $one_transaction['transaction_status'] = "Fail";
                    }
                    $transaction_list .= "<tr><td>Transaction Start Time</td><td>".$one_transaction['transaction_starttime']."</td></tr>";
                    $transaction_list .= "<tr><td>Transaction End Time</td><td>".$one_transaction['transaction_endtime']."</td></tr>";
                    $transaction_list .= "<tr><td>Transaction Result</td><td><b>".$one_transaction['transaction_status']."</b></td></tr>";
                    $transaction_list .= "</table>";
                    $transaction_list .= "<br>";
		}
	}
	else
	{
	$transaction_list = "";
	}
	
	return $transaction_list;
}

function rate_transaction() {
	$transaction_id = $_GET['transaction_id'];
	$username = $_SESSION['username'];
	
	
	$vacancy_list = "<table border=0>";
	$vacancy_list .= "<tr><td>Transaction ID</td><td>".$transaction_id."</td></tr>";
	$vacancy_list .= "<tr><td>This Rating Is By User:</td><td>".$username."</td></tr>";
	$vacancy_list .= "</table>";
	$vacancy_list .= "<form action='do_rate_transaction.php?transaction_id=".$transaction_id."' method='post'>";
	$vacancy_list .= "<table border=1>";
	$vacancy_list .= "<tr><td>Your Rating(Please Enter A Number Between 1 And 5):</td><td><input type='text' name='score' /></td></tr>";
	$vacancy_list .= "<tr><td colspan='2'><input type='submit' /></td></tr>";
	$vacancy_list .= "</table>";
	$vacaycy_list .= "</form>";
	
	return $vacancy_list;
}

function do_rate_transaction() {
	$rate_score = $_POST['score'];
	$user = new User();
	$transaction = new Transaction();
	$transaction_id = $_GET['transaction_id'];
	$user_type = $_SESSION['member_type'];
	$row = $transaction->get_transaction($transaction_id);
	if($user_type==2)
	{
	$rate_for_type_id = $row['tenant_id'];
	$rate_for_user_id = $user->tenant_get_member_id($rate_for_type_id);
	}
	else if($user_type==3)
	{
	$rate_for_type_id = $row['landlord_id'];
	$rate_for_user_id = $user->landlord_get_member_id($rate_for_type_id);
	}
	$rate_score += $user->get_score($rate_for_user_id);
	$rate_result = $transaction->do_rate_transaction($transaction_id, $user_type, $rate_for_user_id, $rate_score);
	return $rate_result;
	}

function update_transaction($transaction_id)
{
	$update_result = "false";
	$transaction = new Transaction();
	$row = $transaction->get_transaction($transaction_id);
	$transaction_status = $row['transaction_status'];
	$transaction_tenant_id = $row['tenant_id'];
	$check = $transaction->check_transaction($transaction_tenant_id);
	if($check == "true")
	{
	$update_result=$transaction->update_transaction($transaction_id,$transaction_status);
	}
	return $update_result;
}

function check_empty_rating() {
        $user = new User();
	$user_type = $_SESSION['member_type'];
        if ($user_type==2) {
            $type_id = $user->get_landlord_id($_SESSION['member_id']);
        }
        else if ($user_type==3){
            $type_id = $user->get_tenant_id($_SESSION['member_id']);
        }
	
	$transaction = new Transaction();
	$countrows = $transaction->check_empty_rating($user_type, $type_id);
	if($countrows>0)
	{
		if($user_type == 2)
		{
                    echo "<br> You haven't finished rating your transactions. Click <a href=view_transaction.php?landlord_id=".$type_id."><b>HERE</b></a> to rate for it!";
		}
		else if($user_type == 3)
		{
                    echo "<br> You haven't finished rating your transactions. Click <a href=view_transaction.php?tenant_id=".$type_id."><b>HERE</b></a> to rate for it!";
		} 
	}
}

function quick_login() 
{
	$username = mysql_escape_string(htmlspecialchars($_POST['username']));
	$password = sha1($_POST['password']);
	$user = new User();
	$user_name = $user->get_user_name($username,$password);
	$user_type = $user->get_member_type($username,$password);
	$user_id = $user->get_member_id($username,$password);
	
	$member_password = $user->get_member_password($username, $password);
	if($user_name){
		if($user_type==2) {
			$type_id = $user->get_landlord_id($user_id);
			echo "Welcome back, " .$username. ", Landlord. <br>";
			$_SESSION['type_id'] = $type_id;
			$_SESSION['username'] = $username;
			$_SESSION['member_type'] = $user_type;
			$_SESSION['member_id'] = $user_id;
			$_SESSION['member_password'] = $member_password;
	
		    echo "Welcome Back, " .$_SESSION['username']. ".<br><br>The webpage will be redirected to your list of requests for transaction in 5 seconds, if not, please click <a href=view_transaction.php?landlord_id=".$type_id.">HERE</a>";
		}
		else if($user_type==3)
		{
			$type_id = $user->get_tenant_id($user_id);
			echo "Welcome back, " .$username. ", Tenant. <br>";
			$_SESSION['type_id'] = $type_id;
			$_SESSION['username'] = $username;
			$_SESSION['member_type'] = $user_type;
			$_SESSION['member_id'] = $user_id;
			$_SESSION['member_password'] = $member_password;
	
		    echo "Welcome Back, " .$_SESSION['username']. ".<br><br>The webpage will be redirected to your list of requests for transaction in 5 seconds, if not, please click <a href=view_transaction.php?tenant_id=".$type_id.">HERE</a>";
		}
	}
	
}
//<--

//--> codes for transaction
function start_transaction_on_map()
{
    $vacancy_id = $_GET['vacancy_id'];
    $vacancy= new Vacancy();
    $landlord_id = $vacancy->get_landlord_id_on_map($vacancy_id);
    return "<a href='request_for_transaction.php?landlord_id=" .$landlord_id. "&vacancy_id=" .$vacancy_id. "'>Interested? Tell the landlord</a><br><a href='index.php'>back</a>";
}
//<--

?>
